/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.widgets;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.widgets.TreeItem;
import org.mags.remas.model.entity.EntityRelationship;
import org.mags.remas.model.entity.ViewType;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class TreeActionTransmitter {
	private static TreeActionTransmitter instance = null;
	private List<RelationshipTreeViewer> relationshipTreeViewerList = new ArrayList<RelationshipTreeViewer>();
	public void add(RelationshipTreeViewer relationshipTreeViewer) {
		relationshipTreeViewerList.add(relationshipTreeViewer);
	}
	public void remove(RelationshipTreeViewer relationshipTreeViewer) {
		relationshipTreeViewerList.remove(relationshipTreeViewer);
	}
	public List<RelationshipTreeViewer> getRelationshipTreeViewerList() {
		return relationshipTreeViewerList;
	}
	public static TreeActionTransmitter getInstance() {
		if(instance==null)
			instance= new TreeActionTransmitter();
		return instance;
	}
	private TreeActionTransmitter() {
	}
	public void addItem(EntityRelationship child, EntityRelationship parent, ViewType viewType) {
		for(RelationshipTreeViewer relationship: relationshipTreeViewerList) {
			if(!relationship.getControl().isDisposed()) {
				if(relationship.getViewType().getViewTypeId()==viewType.getViewTypeId())
					relationship.add(parent, child);
			}
		}
	}
	public void refreshItem(EntityRelationship item) {
		for(RelationshipTreeViewer relationship: relationshipTreeViewerList) {
			if(!relationship.getControl().isDisposed())
				relationship.refresh(item);
		}		
	}
	public void refreshParent(EntityRelationship parent) {
		for(RelationshipTreeViewer relationship: relationshipTreeViewerList) {
			if(!relationship.getControl().isDisposed())
				relationship.refreshParent(parent);
		}				
	}	
	public void removeItem(EntityRelationship item) {
		for(RelationshipTreeViewer relationship: relationshipTreeViewerList) {
			if(!relationship.getControl().isDisposed())
				relationship.remove(item);
		}						
	}	
	private void addTreeItems(TreeItem[] items, List<EntityRelationship> all) {
		for(TreeItem i: items) {
			all.add((EntityRelationship)i.getData());
			addTreeItems(i.getItems(), all);
		}
	}
	public void updateData(EntityRelationship data) {
		for(RelationshipTreeViewer relationship: relationshipTreeViewerList) {
			if(!relationship.getControl().isDisposed())
				relationship.updateData(data);
			List<EntityRelationship> all = new ArrayList<EntityRelationship>();
			all.add(relationship.getRoot());
			addTreeItems(relationship.getTree().getItems(), all);
			for(EntityRelationship i: all) {
				if(i!=null) {
					if(i.getEntity().getEntityId()==data.getEntity().getEntityId()) {
						i.setEntity(data.getEntity());
						relationship.updateData(i);
					}
				}
			}
		}								
	}	
}
